package com.cloud.bc.controller.ajax;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcLikes;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.request.ChatRequest;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.model.response.KarmaListResponse;
import com.cloud.bc.service.BcLikesService;
import com.cloud.bc.service.BcSinglesService;
import com.cloud.bc.service.BcUsersService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@AllArgsConstructor
@RestController
@RequestMapping("m/ajax/karma")
public class Ajax_KarmaController {

    private final BcSinglesService bcSinglesService;
    private final BcUsersService bcUsersService;
    private final BcLikesService bcLikesService;

    //缘份
    @GetMapping("")
    public AjaxResp index(HttpServletRequest request, HttpServletResponse response,
                          Integer page) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        //缘份
        BcSingles bcSinglesCount = new BcSingles();
        bcSinglesCount.setPhotoCheck(1);
        int countAll = bcSinglesService.count(bcSinglesCount);
        KarmaListResponse karmaListResponse = bcSinglesService.selectKarmaList(page);
        if (null == karmaListResponse) {
            //如果是空的，说明就是最后一页了
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("total", page);
            jsonObject.put("currentPage", page);
            ajaxResp.setMap(jsonObject);
            return ajaxResp;
        }
        karmaListResponse.setAge(bcSinglesService.ageConvert(karmaListResponse.getBirthday()));
        if (null != userInfo && Convert.toInt(userInfo.getId(), 0) > 0) {
            BcLikes bcLikes = new BcLikes();
            bcLikes.setUserId(userInfo.getId());
            bcLikes.setTargetId(karmaListResponse.getUserId());
            int count = bcLikesService.count(bcLikes);
            if (count > 0) {
                //说明是以前就喜欢的
                karmaListResponse.setIslike(true);
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", countAll);
        jsonObject.put("currentPage", page);
        ajaxResp.setMap(jsonObject);
        ajaxResp.setList(karmaListResponse);
        return ajaxResp;
    }
}
